﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharePointLogBrowser2
{
    public partial class ViewLogItems : UserControl
    {
        public ViewLogItems()
        {
            InitializeComponent();
        }

        public void SetItemsToListView(LogItemCollection items)
        {
            lvItems.BeginUpdate();
            lvItems.Items.Clear();
            foreach (LogItem item in items)
            {
                ListViewItem lvi = new ListViewItem(new string[] {
                    item.TimeStamp, item.Process, 
                    item.TID, item.Area, 
                    item.Category, item.EventID, 
                    item.Level, item.Message, 
                    item.Correlation});
                lvItems.Items.Add(lvi);
                lvi.Tag = item;
            }
            lvItems.EndUpdate();
        }

        public int GetSelectedIndex()
        {
            if (lvItems.SelectedIndices.Count > 0)
            {
                return lvItems.SelectedIndices[0];
            }
            return -1;
        }

        public LogItem GetSelectedLogItem()
        {
            if (lvItems.SelectedItems.Count > 0)
            {
                ListViewItem lvi = lvItems.SelectedItems[0];
                return lvi.Tag as LogItem;
            }
            return null;
        }

        private void lvItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbxMessage.Text = null;
            LogItem item = GetSelectedLogItem();
            if (item != null)
            {
                tbxMessage.Text = item.Message;
            }
        }

        private void lvItems_DoubleClick(object sender, EventArgs e)
        {
            LogItem item = GetSelectedLogItem();
            if (item != null)
            {
                ViewByCorrelation form = new ViewByCorrelation();
                form.Owner = ParentForm;

                form.Correlation = item.Correlation;

                form.ShowDialog();
            }

        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = FindPrevious(tbxFind.Text, c => c.Message);

            SetSelectedItem(lvi);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            ListViewItem lvi = FindNext(tbxFind.Text, c => c.Message);

            SetSelectedItem(lvi);
        }

        private void SetSelectedItem(ListViewItem lvi)
        {
            if (lvi != null)
            {
                lvItems.SelectedItems.Clear();
                lvi.Selected = true;
                lvi.EnsureVisible();
            }
        }

        private ListViewItem FindNext(string text, GetTextFromLogItem getText)
        {
            int startIndex = GetSelectedIndex();
            for (int i = startIndex + 1; i < lvItems.Items.Count; i++ )
            {
                ListViewItem lvi = lvItems.Items[i];
                LogItem item = lvi.Tag as LogItem;
                if (item != null)
                {
                    if (getText(item).IndexOf(text, StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        return lvi;
                    }
                }
            }
            return null;
        }

        private ListViewItem FindPrevious(string text, GetTextFromLogItem getText)
        {
            int startIndex = GetSelectedIndex();
            for (int i = startIndex - 1; i > 0; i--)
            {
                ListViewItem lvi = lvItems.Items[i];
                LogItem item = lvi.Tag as LogItem;
                if (item != null)
                {
                    if (getText(item).IndexOf(text, StringComparison.InvariantCultureIgnoreCase) >= 0)
                    {
                        return lvi;
                    }
                }
            }
            return null;
        }


        internal void FindNextCorrelation(string correlation)
        {
            ListViewItem lvi = FindNext(correlation, c => c.Correlation);

            SetSelectedItem(lvi);
        }
    }
}
